/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.alsuhaul.bd;

import br.edu.ifal.alsuhaul.modelo.Cliente;
import br.edu.ifal.alsuhaul.modelo.Funcionario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Usuario
 */
public class ClienteBD {

    public Cliente buscarClientePeloCpf(String cpf) throws SQLException {
        Connection fc = null;
        fc = new ConexaoAlsuhaul().abreConexao();
        String sql = "select* from Cliente where cpf = ?";
        PreparedStatement stmt = fc.prepareStatement(sql);
        stmt.setString(1, cpf);
        ResultSet rs = stmt.executeQuery();//usa por que tem retorno
        Cliente c1 = new Cliente();//cria objeto funcionário
        while (rs.next()) {
            c1.setNome(rs.getString("nome"));//pegando informaçõess
            c1.setCidade(rs.getString("cidade"));
            c1.setEndereco(rs.getString("endereco"));
            c1.setEstado(rs.getString("estado"));



        }
        rs.close();
        stmt.close();
        fc.close();//fechando conexão


        return c1;



    }

    public void atualizaCliente(Cliente c1) throws SQLException {
        Connection conn = new ConexaoAlsuhaul().abreConexao();
        String sql = "update Cliente set nome=?,endereco=?,cidade=?,estado=? where cpf=?";
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, c1.getNome());
        st.setString(2, c1.getEndereco());
        st.setString(3, c1.getCidade());
        st.setString(4, c1.getEstado());
        // st.setString(5, c1.getBairro()
        st.setString(5, c1.getCpf());

        st.execute();
        st.close();

    }

    public void removeCliente(Cliente cc) throws SQLException {
        ConexaoAlsuhaul fc = new ConexaoAlsuhaul();
        Connection conn = fc.abreConexao();
        String sql = "delete from Cliente where cpf=?";//executa comando do
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, cc.getCpf());
        stmt.execute();
        stmt.close();
        conn.close();
    }

    public void insereCliente(Cliente c) throws SQLException {
        Connection fc = null;
        try {
            fc = new ConexaoAlsuhaul().abreConexao();
            String sql = "insert into Cliente"
                    + " (cpf,nome,endereco,cidade,estado)"
                    + " values (?,?,?,?,?)";
            PreparedStatement stmt = fc.prepareStatement(sql);
            stmt.setString(1, c.getCpf());
            stmt.setString(2, c.getNome());
            stmt.setString(3, c.getEndereco());
            stmt.setString(4, c.getCidade());
            stmt.setString(5, c.getEstado());
            //stmt.setString(8, c.getBairro());

            stmt.execute();
            stmt.close();
            System.out.println("Gravado!");
            fc.close();

        } catch (SQLException ex) {
            Logger.getLogger(FuncionarioBD.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            fc.close();


        }
    }
}
